package com.desktop.frame;

import com.desktop.entity.Pet;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.Arrays;

/**
 * @author 粥灰灰
 * @created 2022/2/3
 * @version 1.0
 * @Description 逐帧播放的动画面板
 */
public class MyFrame {

    //整体窗口
    private JFrame jFrame = null; //动画展示载体
    private JLabel jLabel = null; //显示图片,作为动画的展示工具

    //以下两个数组只能且必有一个不为空
    private ImageIcon[] imageIcons = null;  //直接把图片载入内存
    private String[] imagePaths = null; //使用路径直接访问

    //窗口运行参数
    private final int petWidth,petHeight;
    private final boolean useRam;//挂件图片加载方式. true: 一次性将所有图片都放入内存中加载; false: 每次只通过路径访问一张图片放入内存加载

    /**
     *
     * @param pet 具体挂件对象信息
     * @param r true：将图片一次性放入内存；false：每隔n毫秒放入一张图片进行加载
     */
    public MyFrame(Pet pet, boolean r,int width,int height) {
        this.jFrame = new JFrame();
        this.jLabel = new JLabel();
        this.petWidth = width;
        this.petHeight = height;
        this.useRam = r;
        initJFrame(pet);
        initImages(pet, r);
        petMoveWithMouse();
    }

    private void initJFrame(Pet pet) {
        //设置外观
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//当窗体被关闭时退出程序
        jFrame.setUndecorated(true);//取消标题栏
        jFrame.setBackground(new Color(0, 0, 0, 0));//背景透明
        jFrame.setType(JFrame.Type.UTILITY);
        jFrame.setAlwaysOnTop(true);//设置位置并显示在最前端
        jFrame.setSize(850, 480);//大小
        jFrame.setLocation(1300, 600);//设置窗体出现位置，基于整个屏幕
        //设置外观

        //添加图标信息
        ImageIcon icon = new ImageIcon("src/resource/" + pet.getPrefix() + '/' + pet.getStart() + pet.getFileType());//图片的路径
        /*int petWidth = icon.getIconWidth();
        int petHeight = icon.getIconHeight();*/
        icon.setImage(icon.getImage().getScaledInstance(petWidth, petHeight, Image.SCALE_AREA_AVERAGING));
        jLabel.setBounds(jFrame.getX(), jFrame.getY(), petWidth, petHeight);
        jLabel.setIcon(icon);
        jFrame.add(jLabel);

    }

    private void petMoveWithMouse() {
        Point origin = new Point(); //鼠标原位
        jFrame.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                origin.setLocation(e.getX(), e.getY());//获取鼠标在挂件中当前按下的位置
            }
        });
        jFrame.addMouseMotionListener(new MouseMotionListener() {

            @Override
            public void mouseMoved(MouseEvent e) {

            }

            @Override
            public void mouseDragged(MouseEvent e) {
                Point p = jFrame.getLocation();
                // 窗口当前的位置 + 鼠标当前在窗口的位置 - 鼠标按下的时候在窗口的位置
                jFrame.setLocation(p.x + e.getX() - origin.x, p.y + e.getY() - origin.y);
            }
        });
        jFrame.validate();
    }

    private void setMouseClickedAction() {
        jFrame.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

            }
        });
    }

    private void initImages(Pet pet, boolean ram) {
        String folderPath = "src/resource/" + pet.getPrefix() + "/";
        int imageCount = pet.getEnd();
        if (ram) {
            imageIcons = new ImageIcon[imageCount + 1];//pet.getEnd()是最后一张的序号，即下标，因为图片是从0开始编号，所以需要+1
            for (int i = 0; i <= imageCount; i++) {
                ImageIcon temp = new ImageIcon(folderPath + i + pet.getFileType());
                temp.setImage(temp.getImage().getScaledInstance(petWidth, petHeight, Image.SCALE_AREA_AVERAGING));
                imageIcons[i] = temp;
            }
        } else {
            imagePaths = new String[imageCount + 1];
            for (int i = 0; i <= imageCount; i++) {
                imagePaths[i] = folderPath + i + pet.getFileType();
            }
        }
    }

    public static void displayPet(JLabel label, ImageIcon icon) {
        label.setIcon(icon);
    }


    private void startAnimation() {

    }

    /**
     * 设置任务栏图标
     */
    public void setTray() {

        if (SystemTray.isSupported())//判断系统是否支持系统托盘
        {
            SystemTray tray = SystemTray.getSystemTray();//获取当前系统的托盘

            //为托盘添加一个右键弹出式菜单
            PopupMenu menu = new PopupMenu();

            MenuItem openItem = new MenuItem("open");//打开
            openItem.addActionListener(e -> jFrame.setVisible(true));

            MenuItem closeItem = new MenuItem("minimize");//最小化
            closeItem.addActionListener(e -> jFrame.setVisible(false));

            MenuItem exItem = new MenuItem("exit");//退出
            exItem.addActionListener(e -> System.exit(0));

            menu.add(openItem);
            menu.addSeparator();
            menu.add(closeItem);
            menu.addSeparator();
            menu.add(exItem);


            ImageIcon icon = new ImageIcon("src/resource/icon.png");

            Image image = icon.getImage().getScaledInstance(icon.getIconWidth(), icon.getIconHeight(), Image.SCALE_DEFAULT);//生成一个任务栏图片对象

            TrayIcon trayIcon = new TrayIcon(image, "伊蕾娜小姐", menu);//设置图标名字
            trayIcon.setImageAutoSize(true);//自适应尺寸*很重要
            trayIcon.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    jFrame.setVisible(true);
                }
            });

            try {
                tray.add(trayIcon);
            } catch (AWTException e) {
                e.printStackTrace();
            }
        }
    }

    public void setVisible(boolean v) {
        jFrame.setVisible(v);//从内存中显示窗体
    }

    public JFrame getjFrame() {
        return jFrame;
    }

    public void setjFrame(JFrame jFrame) {
        this.jFrame = jFrame;
    }

    public JLabel getjLabel() {
        return jLabel;
    }

    public void setjLabel(JLabel jLabel) {
        this.jLabel = jLabel;
    }

    public ImageIcon[] getImageIcons() {
        return imageIcons;
    }

    public String[] getImagePaths() {
        return imagePaths;
    }

    public int getPetWidth() {
        return petWidth;
    }

    public int getPetHeight() {
        return petHeight;
    }

    public boolean isUseRam() {
        return useRam;
    }

    @Override
    public String toString() {
        return "MyFrame{" +
                "jFrame=" + jFrame +
                ", jLabel=" + jLabel +
                ", imageIcons=" + Arrays.toString(imageIcons) +
                ", imagePaths=" + Arrays.toString(imagePaths) +
                ", petWidth=" + petWidth +
                ", petHeight=" + petHeight +
                ", useRam=" + useRam +
                '}';
    }
}
